package mn.idax.exchange.otc.service.impl;

import mn.idax.exchange.otc.constant.CommonConstant;
import mn.idax.exchange.otc.constant.ReferenceExchageConstant;
import mn.idax.exchange.otc.domain.MarketPairs;
import mn.idax.exchange.otc.entity.ExchageMarketPrice;
import mn.idax.exchange.otc.entity.OTCPayModeDetail;
import mn.idax.exchange.otc.entity.OTCPayModeDetailExample;
import mn.idax.exchange.otc.mapper.OTCPayModeDetailMapper;
import mn.idax.exchange.otc.service.OTCPayModeDetailService;
import mn.idax.exchange.otc.util.PercentageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Create by jack_huang on 2018/12/4
 */
@Service
public class OTCPayModeDetailServiceImpl implements OTCPayModeDetailService {

    @Autowired
    private OTCPayModeDetailMapper otcPayModeDetailMapper;
    @Override
    public List<OTCPayModeDetail> getEnablePayModeDetail() {
        OTCPayModeDetailExample example = new OTCPayModeDetailExample();
        example.createCriteria().andIsenableEqualTo(true);
        List<OTCPayModeDetail> otcPayModeDetails = otcPayModeDetailMapper.selectByExample(example);
        return otcPayModeDetails.isEmpty() ? null : otcPayModeDetails;
    }

    /**
     * 获取固定价格关闭的国家
     * @return
     */
    @Override
    public List<OTCPayModeDetail> getClosedFixedSwitch() {
        OTCPayModeDetailExample example = new OTCPayModeDetailExample();
        example.createCriteria().andFixedswitchEqualTo(false);
        List<OTCPayModeDetail> otcPayModeDetails = otcPayModeDetailMapper.selectByExample(example);
        return otcPayModeDetails.isEmpty() ? null : otcPayModeDetails;
    }

    /**
     * 获取固定价格关闭的国家
     * @return
     */
    @Override
    public List<OTCPayModeDetail> getOTCPayModeDetail() {
        OTCPayModeDetailExample example = new OTCPayModeDetailExample();
        List<OTCPayModeDetail> otcPayModeDetails = otcPayModeDetailMapper.selectByExample(example);
        return otcPayModeDetails.isEmpty() ? null : otcPayModeDetails;
    }



    /**
     * 根据不同国家的浮动率 转换价格
     * @return
     */
    @Override
    public void changeFloatingPrice(List<ExchageMarketPrice> exchageMarketPrices, MarketPairs marketPairs) {

        OTCPayModeDetailExample example = new OTCPayModeDetailExample();
        example.createCriteria().andCountryidEqualTo(marketPairs.getCountry());
        List<OTCPayModeDetail> otcPayModeDetails = otcPayModeDetailMapper.selectByExample(example);
        if (!otcPayModeDetails.isEmpty()){

            OTCPayModeDetail otcPayModeDetail = otcPayModeDetails.get(0);

            for (ExchageMarketPrice exchageMarketPrice : exchageMarketPrices) {
                BigDecimal floatingrate = otcPayModeDetail.getFloatingrate();
                if (marketPairs.getMarketPairs().startsWith(ReferenceExchageConstant.BITCNY)){
                    floatingrate = new BigDecimal((String)MemoryService.SYSTEMSETTINGMAP.get(CommonConstant.OTC_BITCNY_FLOATRATE));
                }
                exchageMarketPrice.setFloatingRate(floatingrate);
                exchageMarketPrice.setMinPrice(PercentageUtils.calculation(exchageMarketPrice.getPrice(), floatingrate.multiply(new BigDecimal("-1"))).compareTo(BigDecimal.ZERO) == -1 ?
                        BigDecimal.ZERO : PercentageUtils.calculation(exchageMarketPrice.getPrice(), floatingrate.multiply(new BigDecimal("-1"))));
                exchageMarketPrice.setMaxPrice(PercentageUtils.calculation(exchageMarketPrice.getPrice(), floatingrate));
            }
        }
    }

}
